import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class benchmark_memoria {
    private String cmdLine;

    public benchmark_memoria() {
    	this.cmdLine = "free -m";
	}
    
    public void freeMemory() {
        try {
            String cmdOutput = cmdExec(cmdLine);

            String resultado = cmdOutput.substring(175, 186).trim();
            Float resultInt = new Float( resultado );
            resultInt = resultInt/1000;
    		try {
    			FileWriter resultado_bench_proc = new FileWriter("benchmark_memoria_result.txt");
    			resultado_bench_proc.write(""+resultInt);
    			resultado_bench_proc.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
        } catch (Exception ex) {
            System.err.println("ERROR - invalid response from system ping");
            ex.printStackTrace();
        }
    }
    // run a system command and return output
    public static String cmdExec(String cmdLine) {
        String line;
        String output = "";
        try {
            Process p = Runtime.getRuntime().exec(cmdLine);
            BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
            while ((line = input.readLine()) != null) {
                output += (line);
            }
            input.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return output;
    }	
    public static void main(String[] args) {
    	benchmark_memoria p = new benchmark_memoria();
        p.freeMemory();
    }	
}
